﻿using Project.MODEL;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project.DAL.System
{
    public class OrganizationDal
    {
        /// <summary>
        /// 上下文
        /// </summary>
        PSSEntities entities = DbContextFactory.Create();

        #region Department 部门相关操作

        /// <summary>
        /// 获取部门数据
        /// </summary>
        /// <returns></returns>
        public DbSet<Department> GetDepartment()
        {
            var datas = entities.Department;
            return datas;
        }

        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="dept"></param>
        /// <returns>添加受影响的行</returns>
        public int AddDepartment(Department dept)
        {
            var datas = entities.Department;
            datas.Add(dept);
            return entities.SaveChanges();
        }

        /// <summary>
        /// 修改部门
        /// </summary>
        /// <param name="dept"></param>
        /// <returns>修改受影响的行</returns>
        public int UpdateDepartment(Department dept)
        {
            var datas = entities.Department;
            var data = datas.Where(d => d.deptID == dept.deptID).FirstOrDefault();
            if (data == null)
            {
                return -1; //没有找到修改的对象
            }
            else
            {
                //不能修改的项 就不用赋值
                data.deptName = dept.deptName;
                data.remark = dept.remark;
                data.lastTime = dept.lastTime;

                return entities.SaveChanges();
            }


        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="list"></param>
        /// <returns>删除受影响的行</returns>
        public int DeleteDepartment(Department dept)
        {
            var datas = entities.Department;
            datas.Remove(dept);
            return entities.SaveChanges();
        }

        #endregion

        #region Roles 角色相关操作

        /// <summary>
        /// 获取角色数据
        /// </summary>
        /// <returns></returns>
        public DbSet<Roles> GetRoles()
        {
            var datas = entities.Roles;
            return datas;
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns>添加受影响的行</returns>
        public int AddRoles(Roles role)
        {
            var datas = entities.Roles;
            datas.Add(role);
            return entities.SaveChanges();
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns>修改受影响的行</returns>
        public int UpdateRoles(Roles role)
        {
            var datas = entities.Roles;
            var data = datas.Where(d => d.roleID == role.roleID).FirstOrDefault();
            if (data == null)
            {
                return -1; //没有找到修改的对象
            }
            else
            {
                //不能修改的项 就不用赋值
                data.roleName = role.roleName;
                data.remark = role.remark;
                data.lastTime = role.lastTime;

                return entities.SaveChanges();
            }


        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="list"></param>
        /// <returns>删除受影响的行</returns>
        public int DeleteRoles(Roles role)
        {
            var datas = entities.Roles;
            datas.Remove(role);
            return entities.SaveChanges();
        }

        #endregion


    }
}
